package com.example.lin.util;

import com.example.lin.dto.BagDTO;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * @className: SHA256Util
 * @description:
 * @author: Lin.Gu
 * @date: 2025/8/8
 */
public class SHA256Util {
    public static String encrypt(String data) {
        try {
            //SHA-256 / SHA-512
            MessageDigest md = MessageDigest.getInstance("SHA-512");
            byte[] hashBytes = md.digest(data.getBytes());
            StringBuilder hexString = new StringBuilder();
            for (byte b : hashBytes) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("SHA-256 algorithm not available", e);
        }
    }

    public static void main(String[] args) {
        String originalData = "Hello, World!";
        String encryptedData1 = encrypt(originalData);
        String encryptedData2 = encrypt(originalData);
        System.out.println("原始数据: " + originalData);
        System.out.println("SHA-256加密后: " + encryptedData1 + "-----" + encryptedData2);
        System.out.println("SHA-256加密后: " + encryptedData1.equals(encryptedData2));
    }
}

